package com.wendal.java.happydog;


import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wendal.java.happydog.action.FeedAction;
import com.wendal.java.happydog.bean.PetBean;


public class Main {
	
	public static final String LOGIN_ACTION_URL = "http://passport.renren.com/PLogin.do";
	public static final String LOGIN_REFERER_URL = "http://www.kaixin.com/SysHome.do";
	public static final String POST_TEMPLATE = "email={0}&password={1}&origURL={2}&domain=kaixin.com";
//	public static final String LOGIN_OK_URL = "http://home.kaixin.com/Home.do";
//	public static final String URSER_ID_MARK = "userid";
	
	private Log logger = LogFactory.getLog(getClass());

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Main().login();

	}
	
	public void login(){

		logger.info("开始登陆");
		
		HappyDogConfig config = new HappyDogConfig();
		HttpClientVM clientVM = config.getContent().getClientVM();

		try{
			clientVM.accessURL(LOGIN_REFERER_URL);
		
			Map<String, String> keyValue = new HashMap<String, String>();
			keyValue.put("email", config.getUsername());
			keyValue.put("password", config.getPassword());
			keyValue.put("domain", "kaixin.com");
			keyValue.put("origURL", LOGIN_REFERER_URL);
			
			clientVM.accessURL_Post(LOGIN_ACTION_URL, keyValue,"UTF-8");
			logger.info("登陆成功");
			String ff =	clientVM.accessURL("http://apps.kaixin.com/kxdog_app?origin=104");
			String xx = StringUtils.substringBetween(ff, "<iframe name=\"iframe_canvas\" id=\"iframe_canvas\" src=\"", "\"");
			String xpage = clientVM.accessURL(xx);
//			System.out.println(xpage);
//			System.out.println("iframe = "+xx);
			
			//取环境参数
			{
				config.getContent().xn_sig_api_key = StringUtils.substringBetween(xx, "xn_sig_api_key=","&amp;");
//				System.out.println(config.getContent().xn_sig_api_key);
				config.getContent().tsc = StringUtils.substringBetween(xpage, "tsc:", ",").replace('\'', ' ').trim();
			}
			String myInfoJSon = clientVM.accessURL("http://dog.kaixin.com/pet-profile.do?method=getMyPetsJson&_=");
			
			//Get friends
			String friends_info = clientVM.accessURL("http://dog.kaixin.com/pet-profile.do?method=getMyFriendsAndPetsJSON&uid=0&_=");
			{
				Toolkit.parseFriends(config, friends_info);
			}
//			for (String friend_id : config.getContent().users.keySet()) {
//				System.out.println(config.getContent().users.get(friend_id));
//			}
			{
//				new BuyFoodAction().execute(config.getContent(), "1");
			}
			for (PetBean pet : config.getContent().pets) {
				System.out.println(pet.getName() +" " +pet.getHungry());
				if("true".equals(pet.getHungry())){
					new FeedAction().execute(config.getContent(), "1",pet.getId());
					new FeedAction().execute(config.getContent(), "2",pet.getId());
				}
			}
		}catch (Throwable e) {
			e.printStackTrace();
			return;
		}
	}

	
}
